import storage from "./storage.js";
import st from "./showtoast.js";
import {
	infoVoteData,
	getConfigData
} from "./api.js";

export default {
	/**
	 * 判断是否登陆
	 */
	isLogin() {
		//获取当前路径
		// const path = 
		const pages = getCurrentPages();
		const currentRoute = pages[pages.length - 1].route; // 获取当前页面路由
		const path = '/' + currentRoute;
		// if (storage.getToken() === "") {
		// 	storage.setCurrentPath(path)
		// 	this.goAuthen();
		// 	return false
		// }
		return true;
	},
	/**
	 * 跳转到授权登录
	 */
	goAuthen() {
		// const appID = "wx81718258f32db148";
		const appID = "wxe7acd93af3ad9738";
		const path = "pages/authen/authen";
		const callBackURL = window.location.protocol + "//" + window.location.host + "/#/" + path;

		const url = `https://open.weixin.qq.com/connect/oauth2/authorize?appid=${appID}&redirect_uri=${encodeURIComponent(
			callBackURL
		)}&response_type=code&scope=snsapi_userinfo&state=STATE#wechat_redirect`;
		window.location.replace(url);
	},

	/**
	 * 判断是否跳转到外链
	 * @param {Number} type 1:跳转到外链
	 * @param {String} url 跳转外链的url
	 */
	isGoToExternalLink(type, url) {
		if (url === '') {
			return false;
		}
		if (type == 1) {
			window.location.href = url;
			return false
		}
		return true;
	},


	/**
	 * 投票
	 */
	infoVote(infoID, handler) {
		infoVoteData(infoID).then(res => {
			st.success(res.msg, () => {
				handler();
			})
		})
	},

	/**
	 * 获取配置信息
	 */
	getConfig(handler) {
		getConfigData().then(res => {
			handler(res.data)
		})
	},
	/**
	 * 获取token
	 */
	handleToken(data) {
		if (data.authorization && data.authorization != "") {
			storage.setToken(data.authorization);
		}
	},
	
	/**
	 * 微信支付
	 */
	onWeixinPay(data, handler) {
		//微信支付
		WeixinJSBridge.invoke("getBrandWCPayRequest", Object.assign(data), (result) => {
			if (result.err_msg == "get_brand_wcpay_request:ok") {
				st.success("支付成功", () => {
					handler("success");
				});
			} else {
				st.show("支付失败");
			}
		});
	},
	
	/**
	 * 时间戳转换成年月日
	 */
	timestampToYMD(value) {
		let timestamp = value;
		if (value.toString().length === 10) {
			timestamp = value * 1000;
		}
		let date = new Date(timestamp);
		let year = date.getFullYear();
		let month = (date.getMonth() + 1).toString().padStart(2, "0");
		let day = date.getDate().toString().padStart(2, "0");
		
		let hour = date.getHours().toString().padStart(2, "0");
		let minute = date.getMinutes().toString().padStart(2, "0");
	
		return year + "-" + month + "-" + day + " " + hour + ":" + minute;
	},
	
	/**
	 * 富文本处理
	 */
	handleHTMLData(html) {
		let newContent = html.replace(/\<img/gi, '<img style="width:100% !important;height:auto;display:block;"');
		return newContent;
	},
	
	/**
	 * 报名判断
	 */
	isCanApply(item) {
		if ((item.applyType === 0 && item.hasGrApply === 1 && item.hasTtApply === 1) || (item.applyType === 1 && item.hasGrApply === 1) || (item.applyType === 2 && item.hasTtApply === 1)) {
			return false;
		} else {
			return true;
		}
	},
}